/**
 * @overview 创建标牌
 * @author dengqiang
 * @description Cesium原生
 * @update  2022.11.15
 */

class GWarSign {
  constructor() {
    window.GWarSignArray = []
  }

  //新增单个标注
  add(paramObj) {
    const { name, parentName, option } = paramObj

    const position = window.cesiumMapTool.car3ToWGS84Two(option.position)

    let w = this.strCode(option.label.text) / 4
    w = w > 1.5 ? w : 1.5

    const rectang = Cesium.Rectangle.fromDegrees(
      position.lng -
        window.cesiumMapTool.trsfMeterToXY({ length: 6 }).longitude * 0.5 * w * option.scale,
      position.lat -
        window.cesiumMapTool.trsfMeterToXY({ length: 9 }).latitude * 0.5 * option.scale,
      position.lng +
        window.cesiumMapTool.trsfMeterToXY({ length: 6 }).longitude * 0.5 * w * option.scale,
      position.lat + window.cesiumMapTool.trsfMeterToXY({ length: 9 }).latitude * 0.5 * option.scale
    )

    const stickSignEntity = {
      rectangle: {
        height: position.alt,
        coordinates: rectang,
        material: new Cesium.ImageMaterialProperty({
          image: this.drawCanvas(option.label, option.image.url),
          transparent: true,
          color: { ...Cesium.Color.WHITE },
        }),
        rotation: Cesium.Math.toRadians(option.rotation || 0),
        stRotation: Cesium.Math.toRadians(option.rotation || 0),
      },
    }

    const stickSign = window.map3DControl.viewer.entities.add(stickSignEntity)

    stickSign.attribute = option.attribute || {} //支持添加自定义属性
    stickSign.attribute.name = name
    stickSign.attribute.parentName = parentName
    stickSign.attribute.position = window.cesiumMapTool.car3ToWGS84Two(option.position)
    stickSign.attribute.scale = option.scale
    stickSign.attribute.rotation = option.rotation
    stickSign.attribute.labelText = option.label.text

    const labelObject = {
      name: name,
      parentName: parentName,
      stickSign: stickSign,
    }
    window.GWarSignArray.push(labelObject)
    return stickSign
  }

  //绘制Canvas
  drawCanvas(style, imgUrl) {
    const text = style.text
    const c = document.createElement('canvas')
    const d = (this.strCode(text) / 2) * style.fontSize

    c.width = d > 160 ? d : 160
    c.height = 220

    const ctx = c.getContext('2d')

    const img = new Image()
    img.src = imgUrl

    img.onload = function () {
      //添加图片
      ctx.drawImage(this, c.width / 2 - 160 / 2, 0, 160, 160)

      //添加文字
      ctx.fillStyle = style.color
      ctx.font = 'bold ' + style.fontSize + 'px 微软雅黑' //设置字体
      ctx.textBaseline = 'hanging' //在绘制文本时使用的当前文本基线
      ctx.fillText(text, c.width / 2 - d / 2, 170, d)
    }
    return c
  }

  strCode(str) {
    //获取字符串的字节数
    let count = 0 //初始化字节数递加变量并获取字符串参数的字符个数
    if (str) {
      //如果存在字符串，则执行
      for (let i = 0; i < str.length; i++) {
        //遍历字符串，枚举每个字符
        if (str.charCodeAt(i) > 255) {
          //字符编码大于255，说明是双字节字符(即是中文)
          count += 2 //则累加2个
        } else {
          count++ //否则递加一次
        }
      }
      return count //返回字节数
    }
    return 0 //如果参数为空，则返回0个
  }

  //根据name移除单个消防车标牌
  removeByName(name) {
    window.GWarSignArray.forEach((item, index) => {
      if (item.name == name) {
        window.GWarSignArray.splice(index, 1)
        window.map3DControl.viewer.entities.remove(item.stickSign)
      }
    })
  }

  //根据parentName移除一批消防车标牌
  removeByPatName(parentName) {
    const length = window.GWarSignArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GWarSignArray[i].parentName == parentName) {
        window.map3DControl.viewer.entities.remove(window.GWarSignArray[i].stickSign)
        window.GWarSignArray.splice(i, 1)
      }
    }
  }

  //移除所有消防车标牌
  removeAll() {
    window.GWarSignArray.forEach((item) => {
      window.map3DControl.viewer.entities.remove(item.stickSign)
    })
    window.GWarSignArray = []
  }

  //根据name控制单个消防车标牌的显隐
  setVisibleByName(name, boolean) {
    window.GWarSignArray.forEach((item) => {
      if (item.name == name) {
        item.stickSign.show = boolean
      }
    })
  }

  //根据parentName控制一批消防车标牌的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GWarSignArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.stickSign.show = boolean
      }
    })
  }

  //根据name设置标牌的位置
  setPosition(name, newPosition) {
    window.GWarSignArray.forEach((item) => {
      if (item.name == name) {
        const position = window.cesiumMapTool.car3ToWGS84Two(newPosition)

        const scale = item.stickSign.attribute.scale
        const labelText = item.stickSign.attribute.labelText

        let w = this.strCode(labelText) / 4
        w = w > 1.5 ? w : 1.5

        const rectang = Cesium.Rectangle.fromDegrees(
          position.lng -
            window.cesiumMapTool.trsfMeterToXY({ length: 6 }).longitude * 0.5 * w * scale,
          position.lat - window.cesiumMapTool.trsfMeterToXY({ length: 9 }).latitude * 0.5 * scale,
          position.lng +
            window.cesiumMapTool.trsfMeterToXY({ length: 6 }).longitude * 0.5 * w * scale,
          position.lat + window.cesiumMapTool.trsfMeterToXY({ length: 9 }).latitude * 0.5 * scale
        )

        item.stickSign.rectangle.coordinates = new Cesium.CallbackProperty(function () {
          return rectang
        }, false)
        item.stickSign.rectangle.height = position.alt
      }
    })
  }

  //根据name设置文字的旋转角度
  setRotation(name, rotation) {
    window.GWarSignArray.forEach((item) => {
      if (item.name === name) {
        item.stickSign.rectangle.rotation = Cesium.Math.toRadians(rotation)
        item.stickSign.rectangle.stRotation = Cesium.Math.toRadians(rotation)
      }
    })
  }

  //根据name设置选中效果
  setActive(name, boolean, time = 100) {
    window.GWarSignArray.forEach((item) => {
      if (item.name == name) {
        window.cesiumMapTool.requestAnimationFrame(
          item.stickSign.rectangle.material.color,
          boolean,
          time,
          item.stickSign
        )
      }
    })
  }
}

export default GWarSign
